﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmSuperannuationList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int department_ID=0;
        int branch_ID = 0;
        EmployeeClient wcfEmployee;
        RoleRightInfo listRole;
        MainServiceClient wcfMain;
        public frmSuperannuationList()
        {
            InitializeComponent();
            //LoadLanguage();
            wcfEmployee = Service.Employee();
            wcfMain = Service.MainService();

            listRole = UIParamater.Quyen_CanhBaoSapNghiHuu;

            if (!listRole.Update)
            {
                this.btnViewEmloyee.Image = global::HRM.Properties.Resources.app;
                this.btnViewEmloyee.Tag = "xem";
            }

            txtToDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtToDate.MaxLength = 10;
            cboMonth.SelectedIndex = 2;
            BuildVoucherGrid();
            UISetting.LoadTreeView3(ref tvBranch);
            this.KeyPreview = true;

        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel1);
            UpdateUI.Load_Panel(ref panel2);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Employee_Code", UpdateUI.GetString("maso"), "", 110, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "C", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_Sex", UpdateUI.GetString("gioitinh"), "C", 60, true, true);
            UISetting.AddColumn(ref dgvList, "NewAge", UpdateUI.GetString("tuoi"), "R", 70, true, true);
            //UISetting.AddColumn(ref dgvList, "BRANCH_NAME", UpdateUI.GetString("chinhanh"), "", 200, true, true);
            UISetting.AddColumn(ref dgvList, "DEPARTMENT_NAME", UpdateUI.GetString("phongban"), "", 200, true, true);
        }
        private void LoadData()
        {
            dgvList.Rows.Clear();
            int _month = Convert.ToInt32(cboMonth.Text);
            DateTime _todate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
            int maleage = Convert.ToInt32(txtMaleAge.Text);
            int femaleage = Convert.ToInt32(txtFemaleAge.Text);

            DataSet _ds = wcfEmployee.Employee_Superannuation(out itemCount, _month, branch_ID, department_ID, _todate, maleage, femaleage, currPage, pageSize);
            PageIndex();
            
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_LastName"].ToString()
                    + " " + _ds.Tables[0].Rows[i]["Employee_FirstName"].ToString();
                dgvList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                if (Convert.ToBoolean(_ds.Tables[0].Rows[i]["Employee_Sex"]))
                    dgvList.Rows[i].Cells["Employee_Sex"].Value = "Nam";
                else
                    dgvList.Rows[i].Cells["Employee_Sex"].Value = "Nữ";
                dgvList.Rows[i].Cells["NewAge"].Value = _ds.Tables[0].Rows[i]["NewAge"].ToString();
                //dgvList.Rows[i].Cells["BRANCH_NAME"].Value = _ds.Tables[0].Rows[i]["BRANCH_NAME"].ToString();
                dgvList.Rows[i].Cells["DEPARTMENT_NAME"].Value = _ds.Tables[0].Rows[i]["DEPARTMENT_NAME"].ToString();
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            string _month = cboMonth.Text;
            string _todate = txtToDate.Text;
            string _maleage = txtMaleAge.Text;
            string _femalaage = txtFemaleAge.Text;

            frmPreviewReport f = new frmPreviewReport("DSCB_SapNghiHuu", new string[] { _month, _todate, branch_ID.ToString(), department_ID.ToString(), _maleage, _femalaage });
            f.Show(this);
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
            if (dgvList.Rows.Count > 0)
                btnPrint.Enabled = true;
            else
                btnPrint.Enabled = false;
        }

        private void btnViewEmloyee_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                int Employee_ID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                Form f = new frmEmployeeInfo(Employee_ID,0, 0,UIParamater.Employee_Active, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                    branch_ID = 0;
                else
                    branch_ID = Program.intBranchID;
                department_ID = 0;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void frmSuperannuationList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                
                dgvList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("maso");
                dgvList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvList.Columns["Employee_DateOfBirth"].HeaderText = UpdateUI.GetString("ngaysinh");
                dgvList.Columns["Employee_Sex"].HeaderText = UpdateUI.GetString("gioitinh");
                dgvList.Columns["NewAge"].HeaderText = UpdateUI.GetString("tuoi");
            }
        }

        private void frmSuperannuationList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfMain.Close();
        }
    }
}
